package com.kitty.game.hunqiao;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.EquipField;
import com.kitty.game.hunpo.service.HunPoService;
import com.kitty.game.hunqiao.model.HunQiaoStoreEquipInfo;
import com.kitty.game.hunqiao.model.HunQiaoToSave;
import com.kitty.game.hunqiao.model.HunQiaoZhuRuEquipInfo;
import com.kitty.game.role.model.Role;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
//后加
import com.kitty.game.hunqiao.message.HunQiaoCangKu;
import com.kitty.game.hunqiao.message.HunQiaoFenJieReq;
import com.kitty.game.hunqiao.message.HunQiaoRespEquipInfo;
import com.kitty.game.hunqiao.message.HunQiaoTen;
import com.kitty.game.hunqiao.message.HunQiaoZhuRu;

@Controller
@Slf4j
public class HunQiaoController {

    @Autowired
    private HunPoService hunPoService;

    @Autowired
    private HunQiaoService hunQiaoService;

    /**
     * 魂窍注入、卸下
     * 太阴之气id，如果这个装着就卸下，没装就装上去
     * 21 元性窍 、22 元神窍 23元气窍 24元情窍 25元精窍
     * 神魂3579阶开启
     */
    @RequestMapping
    public void zhuRu(IoSession session, HunQiaoZhuRu request) {

        Role role = SessionUtils.getRoleBySession(session);
        HunQiaoToSave hunQiaoToSave =  hunQiaoService.hunQiaoToSave(role.getRoleId());
        List<HunQiaoStoreEquipInfo> hunQiaoStoreEquipInfos = hunQiaoToSave.getCangKus();

        //判断是否已经注入，相同位置就卸下，不同位置注入同一个就提示
        int itemUnique = request.getItemUnique();
        byte zhuRuWeiZhi = request.getZhuRuWeiZhi();

        for(int pos = 21;pos<=25;pos++ ){
            int[] hunQiao = hunQiaoToSave.getHunQiao();
            if (hunQiao[pos-21]==itemUnique){
                if((byte)0==zhuRuWeiZhi){//位置是0就卸下
                    //注入位置保存
                    hunQiaoToSave.zhuru(pos,0);
                    hunQiaoService.update(role.getRoleId(),hunQiaoToSave);

                    HunQiaoRespEquipInfo hunQiaoZhuRuEquipInfo = new HunQiaoRespEquipInfo();

                    List<HunQiaoZhuRuEquipInfo> list = new ArrayList<>();
                    HunQiaoZhuRuEquipInfo equipInfo = new HunQiaoZhuRuEquipInfo();
                    equipInfo.setPos((byte)pos);

                    list.add(equipInfo);
                    hunQiaoZhuRuEquipInfo.setList(list);
                    MessagePusher.pushMessage(session,hunQiaoZhuRuEquipInfo);

                    //更新人物面板
                    SpringUtils.getRoleService().sendExtraAttribute(role);
                    SpringUtils.getRoleService().sendUpdateShenHunStage(role);
                    //更新面板 魂魄界面的
                    hunPoService.sendPanel(role);
                    return;
                }else{
                    MessagePusher.pushMessage(role, new RespNotifyMiscEx("不可重复注入"));
                    return;
                }
            }
        }
        //注入
        for(HunQiaoStoreEquipInfo hunQiaoStoreEquipInfo:hunQiaoStoreEquipInfos){
            boolean flag = false;
            List<EquipField> equipFields = hunQiaoStoreEquipInfo.getEquipFields();
            for(EquipField equipField:equipFields){
                List<FiedValue> fiedValues = equipField.getFieldValues();
                for(FiedValue fiedValue :fiedValues){
                    if(fiedValue.getType()==84&&fiedValue.getValue().equals(itemUnique)){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }
            if(flag){
                //注入位置保存
                hunQiaoToSave.zhuru(zhuRuWeiZhi,itemUnique);
                hunQiaoService.update(role.getRoleId(),hunQiaoToSave);
                HunQiaoRespEquipInfo hunQiaoZhuRuEquipInfo = new HunQiaoRespEquipInfo();

                List<HunQiaoZhuRuEquipInfo> list = new ArrayList<>();
                HunQiaoZhuRuEquipInfo equipInfo = new HunQiaoZhuRuEquipInfo();
                equipInfo.setPos(zhuRuWeiZhi);
                equipInfo.setEquipFields(hunQiaoStoreEquipInfo.getEquipFields(),hunQiaoStoreEquipInfo.getHunQiaoFields());;//这个这个？

                list.add(equipInfo);
                hunQiaoZhuRuEquipInfo.setList(list);
                hunQiaoZhuRuEquipInfo.addMore();
                MessagePusher.pushMessage(session,hunQiaoZhuRuEquipInfo);
                break;
            }
        }

        //更新人物面板
        SpringUtils.getRoleService().sendExtraAttribute(role);
        SpringUtils.getRoleService().sendUpdateShenHunStage(role);
        //更新面板 魂魄界面的
        hunPoService.sendPanel(role);


    }

    /**
     * 分解太阴之气
     */
    @RequestMapping
    public void fenjie(IoSession session, HunQiaoFenJieReq request) {
        String[] itemUniqueStr = request.getItemUnique().split("\\|");
        Role role = SessionUtils.getRoleBySession(session);
        HunQiaoToSave hunQiaoToSave =  hunQiaoService.hunQiaoToSave(role.getRoleId());
        int[] hunQiao = hunQiaoToSave.getHunQiao();

        List<HunQiaoStoreEquipInfo> hunQiaoStoreEquipInfos = hunQiaoToSave.getCangKus();
        for(String itemUnique:itemUniqueStr){
            for(HunQiaoStoreEquipInfo hunQiaoStoreEquipInfo:hunQiaoStoreEquipInfos){
                boolean flag = false;
                List<EquipField> equipFields = hunQiaoStoreEquipInfo.getEquipFields();
                for(EquipField equipField:equipFields){
                    List<FiedValue> fiedValues = equipField.getFieldValues();
                    for(FiedValue fiedValue :fiedValues){
                        if(fiedValue.getType()==84&&fiedValue.getValue().equals(Integer.parseInt(itemUnique))){
                            flag = true;break;
                        }
                    }
                    if(flag){ break; }
                }
                if(flag){
                    if(Arrays.stream(hunQiao).noneMatch(i->i==Integer.parseInt(itemUnique))){
                        hunQiaoStoreEquipInfos.remove(hunQiaoStoreEquipInfo);

                        //通知前端，清除这个
                        HunQiaoCangKu hunQiaoCangKu = new HunQiaoCangKu();
                        hunQiaoCangKu.setStr("tyzq_store");
                        List<HunQiaoStoreEquipInfo> list = new ArrayList<>();

                        HunQiaoStoreEquipInfo hunQiaoStoreEquipInfo1 = new HunQiaoStoreEquipInfo();
                        hunQiaoStoreEquipInfo1.setPos(hunQiaoStoreEquipInfo.getPos());
                        hunQiaoStoreEquipInfo1.setSize((short)0);
                        list.add(hunQiaoStoreEquipInfo1);
                        hunQiaoCangKu.setList(list);
                        MessagePusher.pushMessage(session,hunQiaoCangKu);
                        //给分解后的东西
                        RespUpdate respUpdate = new RespUpdate();
                        // 阴气之尘 todo 未写
                        respUpdate.setRoleId(role.getRoleId());
                        respUpdate.setList(Collections.singletonList(new FiedValue((short) 473, (byte) 3, 0)));
                        MessagePusher.pushMessage(session,respUpdate);
                    }else{//注入的不分解也是要回这两个包，否则界面卡住
                        HunQiaoCangKu hunQiaoCangKu = new HunQiaoCangKu();
                        hunQiaoCangKu.setStr("tyzq_store");
                        List<HunQiaoStoreEquipInfo> list = new ArrayList<>();
                        list.add(hunQiaoStoreEquipInfo);
                        hunQiaoCangKu.setList(list);
                        hunQiaoCangKu.addMore();
                        MessagePusher.pushMessage(session,hunQiaoCangKu);

                        RespUpdate respUpdate = new RespUpdate();
                        // 阴气之尘 todo 未写
                        respUpdate.setRoleId(role.getRoleId());
                        respUpdate.setList(Collections.singletonList(new FiedValue((short) 473, (byte) 3, 0)));
                        MessagePusher.pushMessage(session,respUpdate);

                        MessagePusher.pushMessage(role, new RespNotifyMiscEx("未分解已注入的太阴之气"));
                    }
                    break;
                }
            }
        }
        hunQiaoService.update(role.getRoleId(),hunQiaoToSave);

    }
    @RequestMapping
    public void ten(IoSession session, HunQiaoTen request){
        Role role = SessionUtils.getRoleBySession(session);
        hunQiaoService.ten(role);
    }

}
